package IU_User;

import clases.Multa;
import clases.Sistema;
import clases.Vehiculo;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class JF_InicioUsuario extends javax.swing.JFrame {
    private Sistema s;
    private ArrayList<Multa> listaMultasPendientes = new ArrayList<>();
    private ArrayList<Multa> listaMultasPagadas = new ArrayList<>();
    private ArrayList<Multa> listaMultas = new ArrayList<>();
    
    public JF_InicioUsuario() throws FileNotFoundException, IOException {
        initComponents();
        s = new Sistema();
        limpiar();
        this.setSize(new Dimension(210, 150));
        JP_Inicio.setVisible(true);
        JP_ListaMultas.setVisible(false);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JP_Inicio = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        JTF_Matricula = new javax.swing.JTextField();
        JB_Consultar = new javax.swing.JButton();
        JP_ListaMultas = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JL_MultasPendientes = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        JTA_MultasPendientes = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        JT_Cuenta = new javax.swing.JTextField();
        JB_Pagar = new javax.swing.JButton();
        JB_Salir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        JL_MultasPagadas = new javax.swing.JList();
        JB_Salir1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        JT_MultasPagadas = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Introduce tu matrícula:");

        JTF_Matricula.setToolTipText("Introduce aquí tu matrícula");

        JB_Consultar.setText("Consultar");
        JB_Consultar.setToolTipText("Consultar");
        JB_Consultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ConsultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JP_InicioLayout = new javax.swing.GroupLayout(JP_Inicio);
        JP_Inicio.setLayout(JP_InicioLayout);
        JP_InicioLayout.setHorizontalGroup(
            JP_InicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_InicioLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(JP_InicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_InicioLayout.createSequentialGroup()
                        .addComponent(JTF_Matricula, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(207, 207, 207))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_InicioLayout.createSequentialGroup()
                        .addComponent(JB_Consultar)
                        .addGap(255, 255, 255))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JP_InicioLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(236, 236, 236))))
        );
        JP_InicioLayout.setVerticalGroup(
            JP_InicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_InicioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(JTF_Matricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(JB_Consultar)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jTabbedPane1.setToolTipText("");

        JL_MultasPendientes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        JL_MultasPendientes.setToolTipText("Listado de multas pendientes");
        JL_MultasPendientes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                JL_MultasPendientesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(JL_MultasPendientes);

        JTA_MultasPendientes.setColumns(20);
        JTA_MultasPendientes.setEditable(false);
        JTA_MultasPendientes.setRows(5);
        JTA_MultasPendientes.setToolTipText("Datos de multa seleccionado");
        jScrollPane2.setViewportView(JTA_MultasPendientes);

        jLabel2.setText("Nº de Cuenta:");

        JT_Cuenta.setToolTipText("Número de cuenta");

        JB_Pagar.setText("Pagar");
        JB_Pagar.setToolTipText("Pagar");
        JB_Pagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_PagarActionPerformed(evt);
            }
        });

        JB_Salir.setText("Salir");
        JB_Salir.setToolTipText("Salir");
        JB_Salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                            .addComponent(JT_Cuenta)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(JB_Pagar))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(JB_Salir)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JT_Cuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JB_Pagar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                        .addComponent(JB_Salir)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addGap(52, 52, 52))))
        );

        jTabbedPane1.addTab("Pendientes", jPanel2);

        JL_MultasPagadas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        JL_MultasPagadas.setToolTipText("Lista de multas pagadas");
        JL_MultasPagadas.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                JL_MultasPagadasValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(JL_MultasPagadas);

        JB_Salir1.setText("Salir");
        JB_Salir1.setToolTipText("Salir");
        JB_Salir1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_Salir1ActionPerformed(evt);
            }
        });

        JT_MultasPagadas.setColumns(20);
        JT_MultasPagadas.setEditable(false);
        JT_MultasPagadas.setRows(5);
        JT_MultasPagadas.setToolTipText("Datos de la multa pagada");
        jScrollPane4.setViewportView(JT_MultasPagadas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(JB_Salir1))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addComponent(jScrollPane4))
                .addGap(18, 18, 18)
                .addComponent(JB_Salir1)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Pagadas", jPanel3);

        javax.swing.GroupLayout JP_ListaMultasLayout = new javax.swing.GroupLayout(JP_ListaMultas);
        JP_ListaMultas.setLayout(JP_ListaMultasLayout);
        JP_ListaMultasLayout.setHorizontalGroup(
            JP_ListaMultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JP_ListaMultasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1))
        );
        JP_ListaMultasLayout.setVerticalGroup(
            JP_ListaMultasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(JP_Inicio, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(JP_ListaMultas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(JP_Inicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(JP_ListaMultas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JB_ConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ConsultarActionPerformed
        Vehiculo vh = buscarMatriculaEnLista(JTF_Matricula.getText());
        if (vh != null) {
            for (Multa m : s.getListaMulta())
                if (m.getMatricula().equals(vh.getMatricula()))
                    listaMultas.add(m);
            if (!listaMultas.isEmpty()) {
                //JOptionPane.showMessageDialog(null, "multas totales" + listaMultas.size());
                cargarListas();
                JP_Inicio.setVisible(false);
                JP_ListaMultas.setVisible(true);     
                this.setSize(new Dimension(600 , 400));
            } else
                JOptionPane.showMessageDialog(null, "El vehículo no tiene multas");
        } else
            JOptionPane.showMessageDialog(null, "Vehículo no encontrado");
    }//GEN-LAST:event_JB_ConsultarActionPerformed

    private void JB_SalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SalirActionPerformed
        JP_ListaMultas.setVisible(false);
        JP_Inicio.setVisible(true);
        this.setSize(new Dimension(210, 150));
        limpiar();
    }//GEN-LAST:event_JB_SalirActionPerformed

    private void JB_Salir1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_Salir1ActionPerformed
        JP_ListaMultas.setVisible(false);
        JP_Inicio.setVisible(true);
        this.setSize(new Dimension(210, 150));
        limpiar();
    }//GEN-LAST:event_JB_Salir1ActionPerformed

    private void JL_MultasPendientesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_JL_MultasPendientesValueChanged
        try{
            JTA_MultasPendientes.setText(JL_MultasPendientes.getSelectedValue().toString());
        }catch(NullPointerException e){}
    }//GEN-LAST:event_JL_MultasPendientesValueChanged

    private void JB_PagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_PagarActionPerformed
        try {
            if (!JT_Cuenta.getText().isEmpty())
                cambiarEstadoMulta((Multa) JL_MultasPendientes.getSelectedValue());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JF_InicioUsuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JF_InicioUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_JB_PagarActionPerformed

    private void JL_MultasPagadasValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_JL_MultasPagadasValueChanged
        try{
            JT_MultasPagadas.setText(JL_MultasPagadas.getSelectedValue().toString());
        }catch(NullPointerException e){}
        
    }//GEN-LAST:event_JL_MultasPagadasValueChanged

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JF_InicioUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JF_InicioUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JF_InicioUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JF_InicioUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new JF_InicioUsuario().setVisible(true);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(JF_InicioUsuario.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(JF_InicioUsuario.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    
    //Funciones
    
    private Vehiculo buscarMatriculaEnLista(String matricula){
        Vehiculo vh = null;
        ArrayList<Vehiculo> listaCoches;
        listaCoches = s.getListaVehiculos();
        for(Vehiculo v : listaCoches)
            if (v.getMatricula().equals(matricula))
                vh = v;
        return vh;
    }
    
    private void limpiar(){
        JL_MultasPendientes.setModel(new DefaultListModel());
        JL_MultasPagadas.setModel(new DefaultListModel());
        listaMultasPagadas.clear();
        listaMultasPendientes.clear();
        JTF_Matricula.setText("");
        JTA_MultasPendientes.setText("");
    }
    
    private DefaultListModel obtenerModelo(ArrayList<Multa> lista){
        DefaultListModel model = new DefaultListModel();
        if (lista != null && !lista.isEmpty()){
            for (Multa m : lista)
                model.addElement(m);
        } 
        return model;
    }
    
    public void cargarListas(){
        listaMultasPagadas.clear();
        listaMultasPendientes.clear();
        for (Multa m : listaMultas){
            if (m.isPagado())
                listaMultasPagadas.add(m);
            else
                listaMultasPendientes.add(m);
        }        
        JL_MultasPendientes.setModel(obtenerModelo(listaMultasPendientes));
        JL_MultasPagadas.setModel(obtenerModelo(listaMultasPagadas));
        JL_MultasPagadas.setSelectedIndex(0);
        JL_MultasPendientes.setSelectedIndex(0);
    }
    
    private void cambiarEstadoMulta(Multa m) throws FileNotFoundException, IOException{
        s.cambiarEstadoMulta(m, true);
        listaMultasPendientes.remove(m);
        listaMultasPagadas.add(m);
        cargarListas();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_Consultar;
    private javax.swing.JButton JB_Pagar;
    private javax.swing.JButton JB_Salir;
    private javax.swing.JButton JB_Salir1;
    private javax.swing.JList JL_MultasPagadas;
    private javax.swing.JList JL_MultasPendientes;
    private javax.swing.JPanel JP_Inicio;
    private javax.swing.JPanel JP_ListaMultas;
    private javax.swing.JTextArea JTA_MultasPendientes;
    private javax.swing.JTextField JTF_Matricula;
    private javax.swing.JTextField JT_Cuenta;
    private javax.swing.JTextArea JT_MultasPagadas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
